import React, { forwardRef, useRef } from 'react'

export default function App() {
    const ref = useRef();
  return (
    <div>
        <button onClick={()=>{
            ref.current.focus();
            ref.current.value = '';
        }}>获取焦点</button>
        <Child ref={ref}></Child>   {/* 子组件上套上ref={} */}
    </div>
  )
}

const Child = forwardRef((props, ref)=>{    //帮助父组件更方便的操控子组件
    return(
    <div>
        <input ref={ref}></input>
    </div>
    )
})